package patrol.mobile.com.mobilepatrol.activity.road_survey;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ExpandableListView;
import android.widget.TextView;

import patrol.mobile.com.mobilepatrol.DBHelper;
import patrol.mobile.com.mobilepatrol.R;
import patrol.mobile.com.mobilepatrol.activity.BaseActivity;
import patrol.mobile.com.mobilepatrol.adapter.InvestDetailCatalogAdapter;
import patrol.mobile.com.mobilepatrol.bean.RouteSurveyTaskStructBO;
import patrol.mobile.com.mobilepatrol.bean.TrafficInvestBaseInfo;
import patrol.mobile.com.mobilepatrol.manager.UserManager;
import patrol.mobile.com.mobilepatrol.utils.CommonUtil;
import patrol.mobile.com.mobilepatrol.utils.StringUtils;
import patrol.mobile.com.mobilepatrol.utils.UIHelper;

/**
 * Created by 健 on 2014/8/26.
 */
public class TrafficInvestDetailCatalogActivity extends BaseActivity {
    private TrafficInvestBaseInfo investBaseInfo;


    private TextView roadLine;
    private TextView investDirect;
    private TextView investUser;

    private TextView roadType;
    private TextView potInfo;
    private ExpandableListView expandableCalalog;
    private InvestDetailCatalogAdapter adapter;
    private CheckBox check_type1;
    private CheckBox check_type2;
    private CheckBox check_type3;
    private CheckBox check_type4;
    private Button back;
    private DBHelper dbHelper;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    public void initData() {
        super.initData();
        dbHelper=new DBHelper(this);
        investBaseInfo= (TrafficInvestBaseInfo) getIntent().getSerializableExtra(UIHelper.INVEST_INFO);

        roadLine.append(investBaseInfo.getSurveyLine());
        roadType.setText(investBaseInfo.getRoudBedType());
        investDirect.append(investBaseInfo.getSurveyWay());
        investUser.append(UserManager.getryName());
        potInfo.setText(investBaseInfo.getStartPot()+investBaseInfo.getStartPotKm()+"+"+investBaseInfo.getStartPotMeter()
                +"至"+investBaseInfo.getEndPot()+investBaseInfo.getEndPotKm2()+"+"+investBaseInfo.getEndPotMeter2());
        int startk = investBaseInfo.getStartPotKm();
        int startm = investBaseInfo.getStartPotMeter();
        int endk = investBaseInfo.getEndPotKm2();
        int endm = investBaseInfo.getEndPotMeter2();



    }

    
    public void setListener() {
        adapter=new InvestDetailCatalogAdapter(TrafficInvestDetailCatalogActivity.this,dbHelper,investBaseInfo);
        expandableCalalog.setAdapter(adapter);

        expandableCalalog.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
            @Override
            public boolean onGroupClick(ExpandableListView expandableListView, View view, int i, long l) {
//
                return false;
            }
        });
        expandableCalalog.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
                RouteSurveyTaskStructBO structBO= (RouteSurveyTaskStructBO) adapter.getGroup(groupPosition);
                if(StringUtils.isEmpty(structBO.getStartPotName()))
                    structBO.setStartPotName(structBO.getName());
                if(StringUtils.isEmpty(structBO.getEndPotName())) {
                    if (structBO.getPotKm() == investBaseInfo.getEndPotKm2()) {
                        structBO.setEndPotName(investBaseInfo.getEndPot()+investBaseInfo.getEndPotKm2()+"+"+investBaseInfo.getEndPotMeter2());
                    }else
                        structBO.setEndPotName(structBO.getPot()+(structBO.getPotKm()+1)+"+"+"000");
                }

                if(investBaseInfo.getEndPotKm2()==structBO.getPotKm()){
                    structBO.setStartPotName(investBaseInfo.getEndPot()+investBaseInfo.getEndPotKm2()+"+000");
                    structBO.setEndPotName(structBO.getPot()+structBO.getPotKm()+"+"+structBO.getPotMeter());
                }
                if(structBO.getRoadWidth()==0){
                    structBO.setRoadLength(getKmVal(structBO.getEndPotName())-getKmVal(structBO.getStartPotName()));
                }

                UIHelper.startInvestDetailActivity(TrafficInvestDetailCatalogActivity.this,structBO.counts.get(childPosition),structBO,investBaseInfo);
                return false;
            }
        });
    }

    private int getKmVal(String str){
        String s=str.substring(1,str.length());
        String[] val= s.split("\\+");

        return Integer.valueOf(val[0])*1000+Integer.valueOf(val[1]);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    public void initView() {
        super.initView();
        roadLine= (TextView) findViewById(R.id.invest_detail_catalog_roadline);
        investDirect= (TextView) findViewById(R.id.invest_detail_catalog_direct);
        investUser= (TextView) findViewById(R.id.invest_detail_catalog_people);
        roadType= (TextView) findViewById(R.id.invest_detail_catalog_roadType);
       // check_type1=(CheckBox)findViewById(R.id.liqing);
       // check_type2=(CheckBox)findViewById(R.id.luji);
        //check_type3=(CheckBox)findViewById(R.id.yanxian);
       // check_type4=(CheckBox)findViewById(R.id.qiaosui);
        potInfo= (TextView) findViewById(R.id.invest_detail_catalog_potInfo);
        expandableCalalog= (ExpandableListView) findViewById(R.id.invest_detail_catalog_expand_catalog);
    }

    @Override
    protected int attachLayoutRes() {
        return R.layout.traffic_invest_detail_catalog_view;
    }

    @Override
    protected void onResume() {
        super.onResume();
        adapter.updateSelectedItem();
        setListener();
    }
    @Override
    public void initTitle() {
        super.initTitle();
        iv_back.setImageResource(R.mipmap.up_white);
        iv_back.setVisibility(View.VISIBLE);
        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                CommonUtil.exitActivityAndBackAnim(TrafficInvestDetailCatalogActivity.this, true);
            }
        });
        tv_center_title.setVisibility(View.VISIBLE);
        tv_center_title.setText("公路技术状况调查明细目录");


    }
}
